//获取应用实例
var app = getApp()
import {
  addAddress,
  editAddress,
  deleteAddress
} from '../../api/address'
Page({
  data: {
    mobile: '',
    name: '',
    id: '',
    title: '',
    isDefault: false,
    region: [''],
    detail: '',
    addValue: ''
  },
  bindCancel: function () {
    wx.navigateBack({})
  },
  async bindSave(e) {
    var that = this;
    var linkMan = e.detail.value.linkMan;
    var address = e.detail.value.address;
    var mobile = e.detail.value.mobile;
    var code = e.detail.value.code;
    if (linkMan == "") {
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel: false
      })
      return
    }
    if (mobile == "") {
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel: false
      })
      return
    }
    if (this.data.region.length <= 0) {
      wx.showModal({
        title: '提示',
        content: '请选择地址',
        showCancel: false
      })
      return
    }
    console.log(that.data.region)
    if (this.data.region[0].indexOf('请选择') >= 0 || this.data.region[1].indexOf('请选择') >= 0 || this.data.region[2].indexOf('请选择') >= 0) {
      wx.showModal({
        title: '提示',
        content: '请选择省、市、区',
        showCancel: false
      })
      return
    }
    if (address == "") {
      wx.showModal({
        title: '提示',
        content: '请填写详细地址',
        showCancel: false
      })
      return
    }
    var apiAddid = that.data.id;
    if (apiAddid) {
      let res = await editAddress(apiAddid, {
        consigneeAddressPath: `${that.data.region[0]},${that.data.region[1]},${that.data.region[2]}`,
        detail: address,
        isDefault: that.data.isDefault,
        mobile,
        name: linkMan
      }, wx.getStorageSync('access_token'))
      console.log(res)
      if (res.code == 10000) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1000,
          success: () => {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1 //返回上一级页面
              })
            }, 1000)
          }
        })
      } else {
        wx.showModal({
          title: '修改失败',
          content: res.msg,
          showCancel: false
        })
      }
    } else {
      let res = await addAddress({
        consigneeAddressPath: `${that.data.region[0]},${that.data.region[1]},${that.data.region[2]}`,
        detail: address,
        isDefault: that.data.isDefault,
        mobile,
        name: linkMan
      }, wx.getStorageSync('access_token'))
      console.log(res)
      if (res.code == 10000) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 1000,
          success: () => {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1 //返回上一级页面
              })
            }, 1000)
          }
        })
      } else {
        wx.showModal({
          title: '添加失败',
          content: res.msg,
          showCancel: false
        })
      }
    }
  },
  changeDefault(e) {
    console.log(e)
    this.setData({
      isDefault: !this.data.isDefault
    })
  },
  bindRegionChange(e) {
    console.log(e.detail.value)
    this.setData({
      region: e.detail.value,
      addValue: e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
    })
  },

  onLoad: function (e) {
    var that = this;
    this.getAddress = this.selectComponent("#getAddress");
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res.pixelRatio) //设备像素比
        // console.log(res.windowWidth) //可使用窗口宽度
        // console.log(res.windowHeight) //可使用窗口高度
        that.setData({
          windowWidth: res.windowWidth, //可使用窗口宽度
          windowHeight: res.windowHeight, //可使用窗口高度
        })
      }
    })
    wx.hideShareMenu()
    if (app.globalData.iphone == true) {
      that.setData({
        iphone: 'iphone'
      })
    }
    console.log(e);
    if (e.id) {
      var id = e.id
      var mobile = e.mobile
      var name = e.name
      var region = JSON.parse(e.region)
      var isDefault = e.default == 'true'
    }

    console.log()
    if (id) {
      // 初始化原数据
      that.setData({
        title: '修改收货地址',
        id,
        mobile,
        name,
        region: region[0].split(' '),
        addValue: region[0],
        isDefault
      })
      console.log(that.data.isDefault)
    } else {
      that.setData({
        title: '添加收货地址'
      })
    }
  },

  focusInput(e){
    this.setData({
      focusName: ''
    })
    console.log(e.currentTarget.dataset.name)
    let focusName = e.currentTarget.dataset.name
    this.setData({
      focusName
    })
  },


  setDBSaveAddressId: function (data) {
    var retSelIdx = 0;
    for (var i = 0; i < commonCityData.cityData.length; i++) {
      if (data.provinceId == commonCityData.cityData[i].id) {
        this.data.selProvinceIndex = i;
        for (var j = 0; j < commonCityData.cityData[i].cityList.length; j++) {
          if (data.cityId == commonCityData.cityData[i].cityList[j].id) {
            this.data.selCityIndex = j;
            for (var k = 0; k < commonCityData.cityData[i].cityList[j].districtList.length; k++) {
              if (data.districtId == commonCityData.cityData[i].cityList[j].districtList[k].id) {
                this.data.selDistrictIndex = k;
              }
            }
          }
        }
      }
    }
  },

  deleteAddress (e) {
    var that = this;
    var Id = e.currentTarget.dataset.id;
    console.log(Id, wx.getStorageSync('access_token'))
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: async (res) => {
        if (res.confirm) {
          let rep = await deleteAddress(Id, wx.getStorageSync('access_token'))
          console.log(rep)
          if (rep.code == 10000) {
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 1000,
              success: () => {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1 //返回上一级页面
                  })
                }, 1000)
              }
            })
          } else {
            wx.showModal({
              title: '删除失败',
              content: rep.msg,
              showCancel: false
            })
          }
        }
      }
    })
  },
})